package com.fastloan.mgt.customer.app;

import com.fastloan.mgt.customer.RedisService;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.login.service.LoginService;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.supers.notice.sms.ISendSms;
import com.kawins.util.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * Created by gelon on 2017/11/9.
 */
@RestController
@RequestMapping("/api/wxapp/customer/reg")
public class AppCustomerRegController extends BaseController<ZyCustomer> {

    @Autowired
    RedisService redisService;

    @Autowired
    ISendSms sendSms;

    @Autowired
    ZyCustomerService customerService;

    @Autowired
    CmuUserService userService;

    @Autowired
    LoginService loginService;

    /**
     * redis 存入key 前缀
     */
    private static final String REG_PRE = "customerReg__";
    private static final String REG_CAN_PRE = "customerRegCan__";

    /**
     * 发送验证码
     */
    @PostMapping("/sms")
    public ResultTo sendSms(String phoneNum) {
        CmuUser user = new CmuUser();
        user.setPhone(phoneNum);
        if (null != userService.selectOne(user)) { //客户手机号不能在user表中
            return new ResultTo(ResultEnum.OPERATION_FAILED, "已存在的手机号");
        }
        String regKey = REG_PRE + phoneNum;
        String canSendKey = REG_CAN_PRE + phoneNum;
        Boolean canSend = (Boolean) redisService.get(canSendKey);
        String content = VerifyCodeUtil.generateTextCode(0, 6, null);
        if (canSend != null) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "离上次发送不到一分钟");
        }
        if (sendSms.send(phoneNum, content)) {
            redisService.set(regKey, content, 5 * 60);
            redisService.set(canSendKey, true, 60);
            log.debug(content);
            return new ResultTo().setData(content);
        } else {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "发送失败");
        }
    }


    /**
     * 注册
     */
    @PostMapping()
    public ResultTo reg(String phoneNum, String code) {
        String regKey = REG_PRE + phoneNum;
        String savedCode = (String) redisService.get(regKey);
        if (savedCode != null && Objects.equals(savedCode, code)) {
            ZyCustomer customer = customerService.selectOne(new ZyCustomer().setPhoneNum(phoneNum));
            if (customer == null) {
                customer = customerService.addByPhone(phoneNum);
            }
            loginService.cusLogin(phoneNum, code, 2, -1);
            return new ResultTo().setData("tokenId", ManagerKit.getSession().getId())
                    .setData("customer", customer);
        } else {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "验证码错误");
        }
    }

}
